Skip to content

Reach a namespaced parent's Python overrides from a namespaced subclass - #180

Merged
quantconnect-kronos[bot] merged 5 commits into
masterfrom
kronos/bug-172-namespace-chain-python-overrides
Nov 23, 2026
Merged

quantconnect-kronos[bot] merged 5 commits into
masterfrom
kronos/bug-172-namespace-chain-python-overrides

Conversation

@quantconnect-kronos

@quantconnect-kronos quantconnect-kronos Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Closes #172

What changed and why

ReflectedClrType.CreateSubclass builds the Python class of a __namespace__ subclass from its generated .NET type, whose reflected members fill the class dict, then copies the class body over them. The generated type has its own members for every virtual member of its base, its overrides included. When the base is itself a __namespace__ Python class, those reflected members sat in the child's dict and hid the Python overrides the parent defines. So PythonDerivedType.InvokeMethod looked up the method, found the managed binding, and treated it as not overridden:

  • for a virtual member, it fell back to the .NET implementation
  • for an abstract or interface member, it threw NotImplementedException: Python object does not have a '<name>' method

The same shadowing hid the parent's methods from Python calls too, so child.foo() also returned the .NET result.

The class dict now drops a name whose reflected methods include a generated forwarder of the base's virtual methods (also through its snake_case alias) when the Python base class resolves that name to a Python (non-managed) attribute and the class body defines neither the name nor any of those methods' .NET names, nor the @clr.clrproperty a get_/set_ accessor among them belongs to. Overloads the generated type doesn't override (such as the static Object.Equals(object, object) beside the virtual Equals(object)) go with it, as the parent's Python override already replaced the whole overload set. Lookups then reach the nearest Python override as Python's method resolution does, both from Python and from .NET (through the generated override's InvokeMethod). Everything else in the class dict stays as before: members the class body defines or declares for .NET (@clr.clrmethod, @clr.clrproperty) with their aliases, constructors and __init__, properties, and members no Python base overrides.

Construction is unchanged: a __namespace__ child still doesn't inherit a __namespace__ parent's Python __init__, as on master, so the managed constructor runs once.

Classes without a Python __namespace__ base are unchanged: their Python base defines no Python overrides of the generated type's members.

Tests

New test_namespace_subclass_of_namespace_subclass in tests/test_subclass.py:

  • a __namespace__ child and grandchild of a __namespace__ subclass of SubClassTest (virtual members): .NET calls (FunctionsTest.test_foo, test_bar, SubClassTest.test_list) and Python calls reach the parent's Python overrides, the grandchild's own bar override wins over the parent's, a member nobody overrides still runs the .NET one, and an instance created from .NET (FunctionsTest.create_instance) behaves the same
  • a __namespace__ child of a __namespace__ implementation of IInterfaceTest (no .NET implementation to fall back to): .NET calls reach the parent's Python methods
  • a __namespace__ child of a __namespace__ System.Object subclass overriding Equals, whose overloads mix the virtual Equals(object) with the static Equals(object, object): Equals from Python, System.Object.Equals(child, other), and reflection's Invoke reach the parent's override
  • a __namespace__ child declaring @clr.clrmethod Calculate and @clr.clrproperty Number over a parent's Python calculate and number: calculate() / number still return the child's members
  • __namespace__ children redeclaring a parent's @clr.clrproperty Number, by named get_Number/set_Number methods and by the decorator with a setter: the property, get_Number()/set_Number() from Python, and reflection's GetValue/SetValue all reach the child's getter and setter
  • a __namespace__ child of a __namespace__ parent whose __init__ calls super().__init__() runs the managed constructor once

Without the runtime change, the test fails with assert 'foo' == 'DerivedClass'; with the previous commit's filter (forwarders only), it fails on the Equals case; with 9d63d13's, it fails on the decorated property's get_Number() (assert 1 == 3).

The CI steps, run locally on Python 3.11 / .NET 10 after pip install -v .:

  • dotnet test --runtime any-x64 src/embed_tests/ --filter "FullyQualifiedName!~SetPythonPath": 1049 passed, 8 skipped
  • dotnet test --runtime any-x64 src/embed_tests/ --filter "FullyQualifiedName~SetPythonPath": 1 passed
  • pytest --runtime netcore tests: 489 passed, 23 skipped
  • dotnet test --runtime any-x64 src/python_tests_runner/: 2 passed

Also tested end to end in a Lean Python backtest whose algorithm defines __namespace__ chains, calls them from .NET and fails unless each result is right: a child of a System.Object subclass overriding ToString, formatted by String.Format, and a child of a System.IComparable implementation, compared by Comparer.Default:

  • With pythonnet 2.0.70, it failed: String.Format returned NamespaceChain172.ChainChild, the .NET default, and the comparison threw TargetInvocationException (NotImplementedException inside).
  • With the change, the algorithm's checks passed: python parent and 42. With the earlier narrowed change (forwarders only), the process then hit InvalidOperationException: GIL must always be released... from Py.GILState.Finalize at shutdown, after the algorithm finished. A build of master hits the same shutdown error in the same setup, so it is not from this change.

The backtest wasn't rerun for the last two commits, which widen the filter to names that also carry non-overridden overloads and keep the accessors of properties the child redeclares; the algorithm's members (ToString, CompareTo) have no other overloads and the algorithm redeclares no property.

The generated type of a __namespace__ subclass reflects its own members,
such as its overrides of the base's virtual methods, into the Python class
dict. Subclassing another __namespace__ class, those members hid the Python
overrides the parent defines, so .NET calls fell back to the .NET
implementation, or threw NotImplementedException for an abstract member.

Drop a reflected member from the new class dict when the Python base class
overrides it in Python, so lookups reach the nearest Python override as
Python's method resolution does.

Closes #172

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@quantconnect-kronos quantconnect-kronos Bot added kronos:develop:working develop: in progress kronos:review Request review labels Nov 23, 2026
@quantconnect-kronos-reviewer quantconnect-kronos-reviewer Bot added kronos:review:queued review: queued kronos:review:working review: in progress and removed kronos:review Request review kronos:review:queued review: queued labels Nov 23, 2026
@quantconnect-kronos-reviewer
quantconnect-kronos-reviewer Bot marked this pull request as ready for review November 23, 2026 19:28

@quantconnect-kronos-reviewer quantconnect-kronos-reviewer Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved. When a Python base class resolves a name to a Python attribute and the class body doesn't define it, the reflected entry is dropped from the class dict. Python's own lookup then reaches the nearest Python override, and PythonDerivedType.InvokeMethod finds it from .NET too. The .NET fallback still goes through reflection (InvokeMember(origMethodName)), so dropping the Python entry doesn't affect it. A single-level __namespace__ class has a reflected .NET type as its base, and that base's lookup finds managed members, so nothing is dropped for it.

I ran extra checks locally against this head and against master. All of these pass here. On master, one crashes and six fail:

  • a grandchild override calling super().foo(): master recurses until the process dies, here it reaches the parent's Python foo
  • a parent's name-mangled private helper (self.__helper()), which on master resolved to the child's generated _P__helper
  • an event implemented in Python on the parent (add_TestEvent), raised from .NET
  • an instance created from .NET (create_instance) of a child without its own members
  • ToString and IComparable.CompareTo reached from .NET through a two-level chain
  • @clr.clrmethod and @clr.clrproperty defined on the parent, used from Python and through reflection on the child

My check that a single-level class still exposes its .NET members passes on both.

Also a behavior change, and the right one: a child whose parent has a Python __init__ now runs it. So Child() raises a TypeError when that __init__ needs arguments, as Parent() already does. Before, it silently skipped the parent's __init__.

Set risk:low: the change is small and easy to revert, its test fails without the runtime change, CI is green (including the Lean regression and unit test runs), and the description reports an end-to-end run that failed before the change and passes with it.

@quantconnect-kronos-reviewer quantconnect-kronos-reviewer Bot added risk:low Low risk and removed kronos:review:working review: in progress labels Nov 23, 2026
@quantconnect-kronos quantconnect-kronos Bot added the kronos:review Request review label Nov 23, 2026
@quantconnect-kronos-reviewer quantconnect-kronos-reviewer Bot added kronos:review:queued review: queued and removed kronos:review Request review labels Nov 23, 2026
@quantconnect-kronos-reviewer-2 quantconnect-kronos-reviewer-2 Bot added kronos:review:working review: in progress and removed kronos:review:queued review: queued labels Nov 23, 2026

@quantconnect-kronos-reviewer-2 quantconnect-kronos-reviewer-2 Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Request changes: the dictionary filter also removes constructor descriptors and aliases for CLR members explicitly declared by the child. I reproduced duplicate managed construction and changed member results against master; both come from pruning more than the virtual forwarding members needed for #172. Details and the narrower scope are inline.

Local validation passed: 489 Python tests, 1,050 embedding tests (8 skipped), and 2 managed Python-runner tests. End-to-end inherited-dispatch assertions also passed, but that process subsequently encountered an independently known shutdown error.

Set risk:high: the broader constructor and member-binding changes introduce regressions despite the passing suites.

Comment thread src/runtime/Types/ReflectedClrType.cs Resolved
@quantconnect-kronos-reviewer-2 quantconnect-kronos-reviewer-2 Bot added risk:high High risk and removed risk:low Low risk kronos:review:working review: in progress labels Nov 23, 2026
The class dict filter also removed the reflected constructor, so a child
inherited its parent's Python __init__, whose super().__init__() then ran the
managed constructor a second time, and the snake_case aliases of members the
child declares for .NET, which then resolved to the parent's Python members.
Remove only the forwarders the generated type adds for the base's virtual
methods, which is what hid the parent's Python overrides.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@quantconnect-kronos
quantconnect-kronos Bot marked this pull request as draft November 23, 2026 19:52
@quantconnect-kronos quantconnect-kronos Bot added the kronos:review Request review label Nov 23, 2026
@quantconnect-kronos-reviewer quantconnect-kronos-reviewer Bot added kronos:review:queued review: queued kronos:review:working review: in progress and removed kronos:review Request review kronos:review:queued review: queued labels Nov 23, 2026

@quantconnect-kronos-reviewer quantconnect-kronos-reviewer Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requesting changes. The narrowed filter fixes both regressions the other review found: the snake_case aliases of members the child declares for .NET keep returning the child's members, and the managed constructor runs once. I checked both locally. But the filter now misses a virtual member that has another overload which isn't a generated forwarder, so the issue's failure is still there for that member. Details are inline.

Locally on this head, pytest --runtime netcore tests passes and CI is green.

risk:high stays: the change leaves a .NET call to a virtual member returning the .NET result, which is the wrong result the issue is about. A fix for that, with a test, would bring this back toward risk:low.

Comment thread src/runtime/Types/ReflectedClrType.cs Resolved
@quantconnect-kronos-reviewer quantconnect-kronos-reviewer Bot removed the kronos:review:working review: in progress label Nov 23, 2026
…oesn't override

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@quantconnect-kronos-reviewer quantconnect-kronos-reviewer Bot added kronos:review:queued review: queued kronos:review:working review: in progress and removed kronos:review Request review kronos:review:queued review: queued labels Nov 23, 2026
@quantconnect-kronos-reviewer
quantconnect-kronos-reviewer Bot marked this pull request as ready for review November 23, 2026 21:21

@quantconnect-kronos-reviewer quantconnect-kronos-reviewer Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved. cba03d4 fixes the other review's accessor finding. A get_/set_ accessor now counts as declared by the class body when the body's value for the property name carries _clr_property_type_. That's the same rule ClassDerivedObject.CreateDerivedType uses (src/runtime/Types/ClassDerived.cs:206-235) to emit those accessors instead of forwarders, so the filter can't mistake one for the other. The new test covers named and decorated redeclarations, with getter and setter, from Python and through reflection. tests/test_subclass.py passes locally on this head, and CI is green.

One optional suggestion is inline.

Set risk:low: the change is small and easy to revert, and it only touches classes whose Python base overrides the member. Each case found in review has a test, CI is green (Lean regression and unit test runs included), and the reported end-to-end run failed before the change and passes with it. That run predates the last two commits, which only keep more members in the dict than before and are covered by the new tests.

Comment thread src/runtime/Types/ReflectedClrType.cs Resolved
@quantconnect-kronos-reviewer quantconnect-kronos-reviewer Bot added risk:low Low risk and removed risk:high High risk kronos:review:working review: in progress labels Nov 23, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@quantconnect-kronos
quantconnect-kronos Bot marked this pull request as draft November 23, 2026 21:26
@quantconnect-kronos quantconnect-kronos Bot added the kronos:review Request review label Nov 23, 2026
@quantconnect-kronos-reviewer quantconnect-kronos-reviewer Bot added kronos:review:queued review: queued kronos:review:working review: in progress and removed kronos:review Request review kronos:review:queued review: queued labels Nov 23, 2026
@quantconnect-kronos-reviewer
quantconnect-kronos-reviewer Bot marked this pull request as ready for review November 23, 2026 21:47

@quantconnect-kronos-reviewer quantconnect-kronos-reviewer Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved. c0b08ef inlines the property-accessor check as isBodyPropertyAccessor, as I suggested last time. It doesn't change behavior: a member counts as declared in the class body under exactly the same conditions as before.

@quantconnect-kronos-reviewer quantconnect-kronos-reviewer Bot removed the kronos:review:working review: in progress label Nov 23, 2026
@quantconnect-kronos quantconnect-kronos Bot added the kronos:review Request review label Nov 23, 2026
@quantconnect-kronos-reviewer quantconnect-kronos-reviewer Bot added kronos:review:queued review: queued and removed kronos:review Request review labels Nov 23, 2026
@quantconnect-kronos-reviewer-2 quantconnect-kronos-reviewer-2 Bot added kronos:review:working review: in progress and removed kronos:review:queued review: queued labels Nov 23, 2026

@quantconnect-kronos-reviewer-2 quantconnect-kronos-reviewer-2 Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved. cba03d4 and c0b08ef resolve my outstanding accessor finding: the filter preserves the special-name getters/setters emitted for a property declared in the child body. The check matches AddPythonProperty, and the new tests exercise named and decorated redeclarations through Python and CLR reflection.

I reproduced the previous getter/setter results of 1/11 on 9d63d13 and verified 3/13 on this head. Local validation passed: 489 Python tests (23 skipped), 1,050 embedding tests (8 skipped), and 2 managed-runner tests.

Tested end to end on this head: inherited ToString, Equals, and CompareTo reach the parent's Python implementations; both property redeclaration forms retain the child's getter and setter results. That run completed cleanly. A broader fixture-backed variant passed its assertions but failed during shutdown on both this head and unchanged master, so that failure is not attributed to this change.

risk:low: the change is small and reversible, the constructor and member-binding regressions are fixed and covered by tests, and the focused end-to-end run verifies the corrected dispatch without changing the working property results.

@quantconnect-kronos-reviewer-2 quantconnect-kronos-reviewer-2 Bot removed the kronos:review:working review: in progress label Nov 23, 2026
@quantconnect-kronos
quantconnect-kronos Bot merged commit a9b04e9 into master Nov 23, 2026
9 checks passed
@quantconnect-kronos
quantconnect-kronos Bot deleted the kronos/bug-172-namespace-chain-python-overrides branch November 23, 2026 22:03
@quantconnect-kronos quantconnect-kronos Bot removed the kronos:develop:working develop: in progress label Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

risk:low Low risk

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A __namespace__ subclass of a __namespace__ subclass doesn't reach its parent's Python overrides from .NET

0 participants